package com.we823.project.controller.repo;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.we823.project.common.utils.CodeGenerate;
import com.we823.project.common.web.StockUtil;
import com.we823.project.common.web.status.StockAction;
import com.we823.project.controller.AbstractBaseController;
import com.we823.project.core.web.WebConstants;
import com.we823.project.model.repo.MaterialModel;
import com.we823.project.model.repo.MaterialStockInModel;
import com.we823.project.repository.service.repo.MaterialService;
import com.we823.project.repository.service.repo.MaterialStockInService;
import com.we823.project.repository.service.repo.MaterialStockService;

@Controller
@RequestMapping(WebConstants.REPO_STOCK+"/in")
public class MaterialStockInController extends AbstractBaseController<MaterialStockInModel> {

	private String baseUrl = WebConstants.REPO_STOCK_TEMPLATE + "in/";
	
	private MaterialStockInService materialStockInService;
	@Autowired
	private MaterialStockService materialStockService;
	@Autowired
	private MaterialService materialService;
	
	@Autowired
	public MaterialStockInController(MaterialStockInService materialStockInService) {
		super(materialStockInService);
		this.materialStockInService = materialStockInService;
		super.baseUrl = baseUrl;
		setResourceIdentity("repo:stock_in");
	}

	@Override
	public Page<MaterialStockInModel> getData(PageRequest pageRequest, HttpServletRequest request, Model model, boolean pageable) {
		Page<MaterialStockInModel> materialStockInPage = null;
		
		materialStockInPage = materialStockInService.findAll(pageRequest);
		fillInfo(materialStockInPage);
		
		return materialStockInPage;
	}

	@Override
	protected Sort getSort(HttpServletRequest request) {
		Sort sort = super.getSort(request);
		if (sort==null) {
			sort = new Sort(Direction.DESC,"addtime");
		}
		return sort;
	}
	private void fillInfo(Page<MaterialStockInModel> materialStockInPage) {
		if (materialStockInPage!=null) {
			List<MaterialStockInModel> materialStockInModels = materialStockInPage.getContent();
			if (materialStockInModels!=null && materialStockInModels.size()>0) {
				Map<String, MaterialModel> materialMap = materialService.map();
				for (MaterialStockInModel materialStockInModel : materialStockInModels) {
					MaterialModel materialModel = materialMap.get(materialStockInModel.getMaterialCode());
					if (materialModel!=null) {
						String name = materialModel.getFullname();
						if (!StringUtils.isEmpty(materialModel.getColor())) {
							name += "("+materialModel.getColor()+")";
						}
						materialStockInModel.setMaterialName(name);
					}
				}
			}
		}
		
	}

	@Override
	public String preAdd(HttpServletRequest request, Model model) {
		model.addAttribute("stockInCode",CodeGenerate.getMaterialStockInCode());
		return super.preAdd(request, model);
	}
	
	@Override
	public String add(HttpServletRequest request, MaterialStockInModel entity, Model model, BindingResult bindingResult,RedirectAttributes redirectAttributes) {
		String result = super.add(request, entity, model, bindingResult,redirectAttributes);
		Date addtime = entity.getAddtime();
		if(addtime!=null){
			String materialCode = entity.getMaterialCode();
			float totalNum = entity.getTotalNum();
			
			StockUtil.changeStock(addtime, materialCode, totalNum,StockAction.IN,materialStockService);
			
		}
		return result;
	}


	public MaterialStockService getMaterialStockService() {
		return materialStockService;
	}

	public void setMaterialStockService(MaterialStockService materialStockService) {
		this.materialStockService = materialStockService;
	}

	public MaterialService getMaterialService() {
		return materialService;
	}

	public void setMaterialService(MaterialService materialService) {
		this.materialService = materialService;
	}
	
	
}
